<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/23
 * Time: 下午3:14
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class BrandController extends AdminBaseController
{
    public function index()
    {
        $curr = I('get.p/d',0);
        $totalNum = 1;
        $word = I('post.word');
        $brand = M('brand');
        $sqlWhere = empty($word) ? '1=1' : 'brand_name like "%'.$word.'%"';
        $brand_list = $brand->page($curr.','.$totalNum)->where($sqlWhere)->select();
        $this->assign('brand_list',$brand_list);
        $count = $brand->where($sqlWhere)->count();
        $Page = new \Think\Page($count,$totalNum);
        $show = $Page->show();
        $this->assign('word',$word);
        $this->assign('page',$show);
        $this->assign('title','FD商城品牌表');
        $this->display();
    }

    /**
     * 添加品牌
     */
    public function add()
    {
        if(IS_AJAX){
            //获取品牌名称
            $brand= I('post.brand','');
            if(empty($brand)){
                $this->ajaxReturn(['state'=>1,'msg'=>'品牌名不能为空！']);
            }
            $brand_id = D('brand')->get_field('id',['brand_name'=>$brand]);
            if($brand_id === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'出现了未知的错误！']);
            }
            if($brand_id){
                $this->ajaxReturn(['state'=>1,'msg'=>'此品牌已经存在！']);
            }
            if(D('brand')->add_data(['brand_name'=>$brand])){
                $this->ajaxReturn(['state'=>0,'msg'=>'添加品牌成功！']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'添加品牌失败！']);
        }else{
            $this->assign('title','FD商城品牌添加');
            $this->display();
        }
    }

    /**
     * 修改品牌
     */
    public function edit()
    {
        if(IS_POST){
            $id = I('post.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            //获取品牌名称
            $brand = I('post.brand','');
            if(empty($brand)){
                $this->ajaxReturn(['state'=>1,'msg'=>'品牌名称不能为空！']);
            }
            if(M('group')->where(['id'=>$id])->setField(['title'=>$title,'up_time'=>time()]) === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'修改成功！']);
        }else{
            $id = I('get.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $brand_info = D('brand')->get_one_info(['id'=>$id],'id,brand_name,sort_order');
            $this->assign('brand_info',$brand_info);
            $this->assign('title','FD商城品牌修改');
            $this->display();
        }
    }

    /**
     * 删除品牌
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            if(M('Goods')->where(['bid' => $id])){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除有对应商品的品牌！']);
            }
            if(M('group')->where(['id'=>$id])->getField('id') == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除不存在的角色！']);
            }
            if(M('group')->delete($id) == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'删除失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功！']);
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }
}